package com.example.service.serviceImpl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.Result;


import com.example.common.UserHolder;
import com.example.domain.entity.*;
import com.example.mapper.NoticeMapper;
import com.example.mapper.OrdersMapper;
import com.example.mapper.ReserveMapper;
import com.example.mapper.RoomMapper;
import com.example.service.ReserveService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ReserveServiceAImpl implements ReserveService {

//    @Resource
//    private ReserveDao reserveDao;

    @Autowired
    private ReserveMapper reserveMapper;
//
//    @Resource
//    private RoomDao roomDao;

    @Autowired
    private RoomMapper roomMapper;

//    @Autowired
//    private NoticeDao noticeDao;

    @Autowired
    private NoticeMapper noticeMapper;

    @Autowired
    private OrdersMapper ordersMapper;

    @Override
    public List<Reserve> findAll() {
        return reserveMapper.selectList(null);
    }

    @Override
    public Page<Reserve> findBySearch(Params params) {
        // 创建 MyBatis-Plus 的分页对象
        Page<Reserve> page = new Page<>(params.getPageNum(), params.getPageSize());
        // 创建查询条件包装器
        QueryWrapper<Reserve> queryWrapper = new QueryWrapper<>();

        Account user = UserHolder.getUser();
        // 根据 params 构建查询条件
        if (params != null) {
            if (params.getName() != null && !params.getName().isEmpty()) {
                queryWrapper.like("name", params.getName());
            }
            queryWrapper.eq(user.getRole().equals("USER"),"user_id",user.getId() );
        }

        // 按 id 倒序排序
        queryWrapper.orderByDesc("id");

        // 执行分页查询
        Page<Reserve> reservePage = reserveMapper.selectPage(page, queryWrapper);
        List<Reserve> records = reservePage.getRecords();
        for (Reserve reserve : records) {
            Integer roomId = reserve.getRoomId();
            List<Orders> list =  ordersMapper.selectList(new QueryWrapper<Orders>().eq("room_id", roomId));
            if(list==null){
                continue;
            }
            Orders orders = list.get(0);
            reserve.setPayStatus(orders.getStatus());
            reserve.setOrderId(orders.getOrderId());
            reserve.setPaymentDeadline(orders.getPaymentDeadline());
        }

        reservePage.setRecords(records);

        // 将 MyBatis-Plus 的 Page 对象转换为 PageInfo 对象
        return reservePage;
    }

    @Override
    public Result add(Reserve reserve) {
        reserve.setReserveStatus(0);
        reserveMapper.insert(reserve);
        //todo
//        Room room = new Room();
//        room.setId(reserve.getRoomId());
//        room.setStatus(1);
//        roomMapper.updateById(room);
        return Result.success();
    }

    @Override
    public void update1(Reserve reserve) {
        Notice notice = new Notice();
        notice.setName(reserve.getUserName());
//        notice.setDescription("预约" + reserve.getName() + "自习室成功，请留意预约时间进行使用");
        notice.setDescription("取消预约" + reserve.getName() + "自习室成功");
        notice.setTime(DateUtil.now());
        notice.setUserId(reserve.getUserId());
        noticeMapper.insert(notice);

//        reserve.setReserveStatus(1);
//        reserve.setUseStatus(1);
//        reserveMapper.updateById(reserve);
        LambdaUpdateWrapper<Orders> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Orders::getOrderId,reserve.getOrderId())
                     .eq(Orders::getStatus,5)
                     .set(Orders::getStatus,6);
        ordersMapper.update(null,updateWrapper);
    }

    @Override
    public void update2(Reserve reserve) {
        Notice notice = new Notice();
        notice.setName(reserve.getUserName());
        notice.setDescription("预约" + reserve.getName() + "自习室失败，管理员审核不通过");
        notice.setTime(DateUtil.now());
        notice.setUserId(reserve.getUserId());
        noticeMapper.insert(notice);

        reserve.setReserveStatus(2);
        reserve.setUseStatus(2);
        reserveMapper.updateById(reserve);
        roomMapper.updateByIdToKong(reserve.getRoomId());
    }

    @Override
    public void update3(Reserve reserve) {
        reserve.setUseStatus(3);
        reserveMapper.updateById(reserve);
        roomMapper.updateByIdToKong(reserve.getRoomId());
    }

    @Override
    public void update4(Reserve reserve) {
        LambdaUpdateWrapper<Orders> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Orders::getOrderId,reserve.getOrderId())
                     .eq(Orders::getStatus,2)
                     .set(Orders::getStatus,5);
        ordersMapper.update(null,updateWrapper);
//        reserve.setReserveStatus(1);
//        reserve.setUseStatus(3);
//        reserveMapper.updateById(reserve);
//        roomMapper.updateByIdToKong(reserve.getRoomId());
    }

    @Override
    public void confirm(Reserve reserve) {
        //订单置为已取消状态
        LambdaUpdateWrapper<Orders> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Orders::getRoomId,reserve.getRoomId())
                     .eq(Orders::getStatus,6)
                     .set(Orders::getStatus,3);
        ordersMapper.update(null,updateWrapper);
        //会议室置为空闲状态
        LambdaUpdateWrapper<Room> roomLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        roomLambdaUpdateWrapper.eq(Room::getId,reserve.getRoomId())
                               .set(Room::getStatus,0);
        roomMapper.update(null,roomLambdaUpdateWrapper);
    }


    @Override
    public void delete(Integer id) {
        reserveMapper.deleteById(id);
    }

    @Override
    public Reserve findById(Integer id) {
        return reserveMapper.selectById(id);
    }


}